﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VMind.Global;
using PPIDesktopClient.Logic;
using PPIDesktopClient.General;
using PPIDesktopClient.Entities;

namespace PPIDesktopClient
{
    public partial class frmAssignUtility : frmDialogBase
    {
        private int m_SelectedVendorId;
        public int SelectedVendorId
        {
            set
            {
                m_SelectedVendorId = value;
            }
        }

        public frmAssignUtility()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Validate() && ValidateUtility())
            {
                this.DialogResult = DialogResult.OK;
                int idUtility = ConvertHelper.ToInt32(txtUtilityAssetId.Text);
                LVendor_Visit_XMissionStructure.UpdateUtilty(m_SelectedVendorId, idUtility);
            }

        }

        private bool ValidateUtility()
        {
            bool _Founded = false;
            List<EUtility_Asset_XMissionStructure> _UtilityList = LUtility_Asset_XMissionStructure.SelectUtilityNearVendor(m_SelectedVendorId, Constants.K_FEETDISTANCE);
            int idUtility = ConvertHelper.ToInt32(txtUtilityAssetId.Text);
            foreach (EUtility_Asset_XMissionStructure u in _UtilityList)
            {
                if (u.Id == idUtility)
                {
                    _Founded = true;
                    break;
                }
            }
            if (!_Founded)
            {
                VAL_txtUtilityId.Visible = true;
            }
            else
            {
                VAL_txtUtilityId.Visible = false;
            }
            return _Founded;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void frmAssignUtility_Load(object sender, EventArgs e)
        {
            ClearControls();
        }

        private void ClearControls()
        {
            txtUtilityAssetId.Clear();
            VAL_txtUtilityId.Visible = false;
        }
    }
}
